package com.yb0os1;

import java.util.Date;

public class Main {
    final static int N = 100;
    static  int count = 0;

    final static Object Lock = new Object();

    public static void main(String[] args) {
        Thread thread1 = new Thread(new Seq(0));
        Thread thread2 = new Thread(new Seq(1));
        Thread thread3 = new Thread(new Seq(2));
        thread1.start();
        thread2.start();
        thread3.start();
    }

    static class Seq implements Runnable {
        int i;
        public Seq(int i) {
            this.i = i;
        }

        @Override
        public void run() {
            while(count< N){
                synchronized (Lock){
                    while (count % 3 != i){//当前线程不是应该执行的线程
                        try {
                            Lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    //
                    if (count > N){
                        Lock.notifyAll();
                        return;
                    }
                    System.out.println(Thread.currentThread().getName() + " " + count);
                    count++;
                    Lock.notifyAll();
                }
            }
        }
    }
}
